{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydrake.common.value import Value\n",
    "from pydrake.math import RigidTransform\n",
    "from pydrake.systems.primitives import ConstantValueSource\n",
    "\n",
    "def _create_context_value_reference():\n",
    "    \"\"\"Returns a reference to a C++ object whose storage comes from inside a Context.\n",
    "    The return value should keep the entire (otherwise-unused) context alive.\n",
    "    \"\"\"\n",
    "    system = ConstantValueSource(Value(RigidTransform()))\n",
    "    context = system.CreateDefaultContext()\n",
    "    return system.get_output_port().Eval(context)\n",
    "\n",
    "# Check that the object still retains its correct value.\n",
    "X = _create_context_value_reference()\n",
    "print(X)\n",
    "assert X.IsExactlyIdentity()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Re-check that the object still retains its correct value.\n",
    "print(X)\n",
    "assert X.IsExactlyIdentity()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
